package com.example.common;

import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.example.entity.Admin;
import com.example.exception.CustomException;
import com.example.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class JwtInterceptor implements HandlerInterceptor {
    private static final Logger log = LoggerFactory.getLogger(JwtInterceptor.class);
    @Resource
    private AdminService adminService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response ,Object handle){
        //获取
        String token =request.getHeader("token");
        if(StrUtil.isBlank(token)){
            token =request.getParameter("token");
        }
        //认证
        if(StrUtil.isBlank(token)){
            throw new CustomException("无token，请重新登录");
        }
        String adminId;
        Admin admin;
        try {
            adminId = JWT.decode(token).getAudience().get(0);
            admin = adminService.findByid(Integer.parseInt(adminId));
        }catch (Exception e){
            String errMsg ="token验证失败";
            log.error(errMsg+"，token="+token,e);
            throw new CustomException(errMsg);
        }
        if(admin==null){
            throw new CustomException("用户不存在");
        }
        try {
            JWTVerifier jwtVerifier =JWT.require(Algorithm.HMAC256(admin.getPassword())).build();
            jwtVerifier.verify(token);
        }catch (JWTVerificationException e){
            throw new CustomException("token验证失败");
        }
//        log.info("token验证成功");
        return true;
    }
}
